﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net.NetworkInformation;
using GlobalRadar.Language;

namespace GlobalRadar.UI
{
    public partial class AppLogin : PhoneApplicationPage
    {
        AppStatusBar stastus = new AppStatusBar();

        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        public AppLogin()
        {
            InitializeComponent();

            if (NetworkInterface.GetIsNetworkAvailable() != true)

                MessageBox.Show(App.LangVN == true?ResourceVN.NetworkError:ResourceEn.NetworkError);

            LoadLanguage();
        }

        private void LoadLanguage()
        {
            if (App.LangVN == true)
            {
                title.Text = ResourceVN.LoginTitle;
                Caption.Text = ResourceVN.LoginCaption;
                lbUsername.Text = ResourceVN.LoginLabelUser;
                lbPassword.Text = ResourceVN.LoginLabelPass;
                lbRegister.Text = ResourceVN.LoginLabelRegister;
                btnLogin.Content = ResourceVN.LoginButtonLogin;
                btnCancel.Content = ResourceVN.LoginButtonExit;
            }
            else
            {
                title.Text = ResourceEn.LoginTitle;
                Caption.Text = ResourceEn.LoginCaption;
                lbUsername.Text = ResourceEn.LoginLabelUser;
                lbPassword.Text = ResourceEn.LoginLabelPass;
                lbRegister.Text = ResourceEn.LoginLabelRegister;
                btnLogin.Content = ResourceEn.LoginButtonLogin;
                btnCancel.Content = ResourceEn.LoginButtonExit;
            }
        }
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.RelativeOrAbsolute));
        }

        private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/UI/AppRegister.xaml", UriKind.RelativeOrAbsolute));
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable() != true)
            {
                MessageBox.Show(App.LangVN == true ? ResourceVN.NetworkError : ResourceEn.NetworkError);

                return;
            }
            stastus.ShowPopup(App.LangVN == true ? ResourceVN.LoginWaitConnect : ResourceEn.LoginWaitConnect);

            mySQLService.RadarServicePortTypeClient client = new mySQLService.RadarServicePortTypeClient();

            client.CheckLoginCompleted += (s, args) =>
            {
                stastus.Remove();

                if (args.Result == "false")
                {

                    MessageBox.Show(App.LangVN == true ? ResourceVN.LoginErrorContent : ResourceEn.LoginErrorContent, App.LangVN == true ? ResourceVN.LoginErrorCaption : ResourceEn.LoginErrorCaption, MessageBoxButton.OK);
                }
                else
                {
                    var Data = Json.Parse(args.Result);

                    MessageBox.Show(App.LangVN == true ? ResourceVN.LoginSucessContent : ResourceEn.LoginSucessContent, App.LangVN == true ? ResourceVN.LoginSucessCaption : ResourceEn.LoginSucessCaption, MessageBoxButton.OK);
        
                    Dictionary<string, string> logins = Json.Parse(args.Result);

                    if (logins.ContainsKey("UserID") == true) int.TryParse(logins["UserID"], out Global.UserInfo.UserID);

                    if (logins.ContainsKey("Username")) Global.UserInfo.Username = logins["Username"];

                    if (logins.ContainsKey("Password")) Global.UserInfo.Password = logins["Password"];

                    if (logins.ContainsKey("Fullname")) Global.UserInfo.Fullname = logins["Fullname"];

                    if (logins.ContainsKey("Email")) Global.UserInfo.Email = logins["Email"];

                    if (logins.ContainsKey("Permission")) int.TryParse(logins["Permission"], out Global.UserInfo.Permission);

                    if (logins.ContainsKey("Status")) int.TryParse(logins["Status"], out Global.UserInfo.Status);

                    settings["Login"] = true;

                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
                }
                
             };
            client.CheckLoginAsync(txtUsername.Text, txtPassword.Password.ToString());
        }
    }
}